// service/user_service/user_login_lose.go
package user_service

import (
	"errors"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
)

type UserLoginLoseOptions struct {
	UserID uint
	ID     uint
}

func (u UserService) UserLoginLose(option UserLoginLoseOptions) (err error) {
	var userLogin models.UserLoginModel

	where := global.DB.Where("id = ?", option.ID)
	if option.UserID != 0 {
		where.Where("user_id = ?", option.UserID)
	}

	err = global.DB.Where(where).Take(&userLogin).Error
	if err != nil {
		err = errors.New("记录不存在")
		return
	}

	global.DB.Model(&userLogin).Update("lose", true)
	claims, _ := jwts.ParseToken(userLogin.Token)
	u.RedisService.SetLogoutToken(ctype.UserHandleLogout, userLogin.Token, claims.ExpiresAt)
	return nil
}
